package com.spark.data.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import com.spark.colliery.common.ext.gone
import com.spark.data.R
import com.spark.data.model.PersonalFolderListModel

/**
 *
 */
class PersonalFolderAdapter(var file: PersonalFolderListModel?) : BaseExpandableListAdapter() {
    private val groupStateList = ArrayList<Boolean>()
    private val isOpen: BooleanArray
    private var onFileSelectedListener: OnFileSelectedListener? = null
    val results: MutableList<PersonalFolderListModel.ResultMap.File> = mutableListOf()


    init {
        file?.resultMaps?.forEach {
            groupStateList.add(false)
        }
        isOpen = BooleanArray(file?.resultMaps?.size ?: 0)

    }

    override fun getGroup(groupPosition: Int): Any {
        return file?.resultMaps?.get(groupPosition) ?: Any()
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
        return true
    }

    override fun hasStableIds(): Boolean {
        return true
    }

    @SuppressLint("SetTextI18n")
    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View {
        var converter = convertView
        var parentHolder: ParentHolder

        if (converter == null) {
            converter = LayoutInflater.from(parent?.context).inflate(R.layout.item_file_one, null)
            parentHolder = ParentHolder(converter)
            converter.tag = parentHolder
        } else {
            parentHolder = converter.tag as ParentHolder
        }
        val groupData = getGroup(groupPosition) as PersonalFolderListModel.ResultMap
        parentHolder.apply {
            tvFolder.text = groupData.name
            tvFileCount.text = "${groupData.fileList.size}个"
            ivArr.setImageResource(if (groupStateList[groupPosition]) R.drawable.ic_arrow_down_blue else R.drawable.ic_arrow_up_blue)
        }
        return converter ?: View(parent?.context)
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return file?.resultMaps?.get(groupPosition)?.fileList?.size ?: 0
    }

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return file?.resultMaps?.get(groupPosition)?.fileList?.get(childPosition) ?: 0
    }

    override fun getGroupId(groupPosition: Int): Long {
        return groupPosition.toLong()
    }

    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View {
        var converter = convertView
        var childHolder: ChildHolder
        if (converter == null) {
            converter = LayoutInflater.from(parent?.context).inflate(R.layout.item_file_two, null)
            childHolder = ChildHolder(converter)
            converter.tag = childHolder
        } else {
            childHolder = converter.tag as ChildHolder
        }
        val childData = getChild(groupPosition, childPosition) as PersonalFolderListModel.ResultMap.File
        childHolder.apply {
            cb.isChecked = childData.isSelected
            tvFile.text = childData.filename
            blockDivider.gone(childPosition != getChildrenCount(groupPosition) - 1)
            rootView.setOnClickListener {
                val beforeState = cb.isChecked
                cb.isChecked = !beforeState
                onFileSelectedListener?.onSelected(groupPosition, childPosition)
            }
            cb.setOnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) {
                    if (!results.contains(childData))
                        results.add(childData)
                } else {
                    results.remove(childData)
                }
            }
        }
        return converter ?: View(parent?.context)
    }

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
        return childPosition.toLong()
    }

    override fun getGroupCount(): Int {
        return file?.resultMaps?.size ?: 0
    }

    internal class ParentHolder(var rootView: View) {
        var tvFolder: TextView = rootView.findViewById(R.id.tv_folder_name)
        var tvFileCount: TextView = rootView.findViewById(R.id.tv_file_count)
        var ivArr: ImageView = rootView.findViewById(R.id.iv_arr)
    }

    internal class ChildHolder(var rootView: View) {
        var cb: CheckBox = rootView.findViewById(R.id.cb)
        var tvFile: TextView = rootView.findViewById(R.id.tv_name)
        var ivEye: ImageView = rootView.findViewById(R.id.iv_eye)
        var lineDivider: View = rootView.findViewById(R.id.file_divider)
        var blockDivider: View = rootView.findViewById(R.id.block_divider)
    }

    fun addOnFileSelectedListener(listener: (Int, Int) -> Unit) {
        onFileSelectedListener = object : OnFileSelectedListener {
            override fun onSelected(groupPosition: Int, childPosition: Int) {
                listener(groupPosition, childPosition)
            }

        }
    }

    interface OnFileSelectedListener {
        fun onSelected(groupPosition: Int, childPosition: Int)
    }
}